package com.dly.hope.controller;

import com.dly.hope.annotation.LoginRole;
import com.dly.hope.constant.R;
import com.dly.hope.model.ro.AddGroupUserRo;
import com.dly.hope.model.ro.CreateGroupRo;
import com.dly.hope.model.ro.KickOutRo;
import com.dly.hope.model.ro.UpdateGroupRo;
import com.dly.hope.model.vo.*;
import com.dly.hope.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "群聊相关接口")
@RequestMapping(value = "/group")
public class GroupController extends BaseController{


    @Resource
    private GroupService groupService;


    @GetMapping ("/getGroup")
    @ApiOperation(value = "获取自己存在的群")
    @LoginRole
    public ResponseEntity<R<List<GroupVo>>> getOtherGroup(){
        return operate(()->this.successDate(groupService.getGroup(getCurrentUserId())));
    }

    @GetMapping("/getGroupAddMessage")
    @ApiOperation(value = "获取群组添加信息")
    @LoginRole
    public ResponseEntity<R<List<GroupAddMessageVo>>> getFriendAddMessage(){
        return operate(()->this.successDate(groupService.getFriendAddMessage(getCurrentUserId())));
    }

    @PostMapping ("/intoGroup")
    @ApiOperation(value = "申请加入群聊")
    @LoginRole
    public ResponseEntity<R<Void>> intoGroup(String groupNumber){
        groupService.intoGroup(getCurrentUserId(), groupNumber);
        return operate(this::success);
    }

    @PostMapping ("/addGroupUser")
    @ApiOperation(value = "邀请加入",consumes = MediaType.APPLICATION_JSON_VALUE)
    @LoginRole
    public ResponseEntity<R<Void>> addGroupUser(@RequestBody AddGroupUserRo addGroupUserRo){
        groupService.addGroupUser(getCurrentUserId(), addGroupUserRo);
        return operate(this::success);
    }

    @PostMapping("/agree")
    @ApiOperation(value = "同意添加")
    @LoginRole
    public ResponseEntity<R<Void>> agreeGroupUser(String id){
        groupService.agreeGroupUser(getCurrentUserId(), id);
        return operate(this::success);
    }

    @PostMapping("/refusal")
    @ApiOperation(value = "拒绝添加")
    @LoginRole
    public ResponseEntity<R<Void>> refusalGroupUser(String id){
        groupService.refusalFriend(getCurrentUserId(), id);
        return operate(this::success);
    }

    @PostMapping ("/createGroup")
    @ApiOperation(value = "创建群聊")
    @LoginRole
    public ResponseEntity<R<String>> createGroup(CreateGroupRo createGroupRo){
        return operate(()->this.successDate(groupService.createGroup(getCurrentUserId(), createGroupRo)));
    }

    @GetMapping ("/getGroupInfo")
    @ApiOperation(value = "获取群组信息")
    @LoginRole
    public ResponseEntity<R<GroupVo>> getGroupInfo(String groupId){
        return operate(()->this.successDate(groupService.getGroupInfo(groupId)));
    }

    @GetMapping ("/getGroupMember")
    @ApiOperation(value = "获取群成员")
    @LoginRole
    public ResponseEntity<R<List<MemberVo>>> getGroupMember(String groupId){
        return operate(()->this.successDate(groupService.getGroupMember(groupId)));
    }

    @PostMapping ("/exitGroup")
    @ApiOperation(value = "退出该群/解散群聊")
    @LoginRole
    public ResponseEntity<R<Void>> exitGroup(String groupId){
        groupService.exitGroup(getCurrentUserId(), groupId);
        return operate(this::success);
    }

    @PostMapping ("/updateGroup")
    @ApiOperation(value = "修改群聊")
    @LoginRole
    public ResponseEntity<R<Void>> updateGroup(UpdateGroupRo updateGroupRo){
        groupService.updateGroup(getCurrentUserId(), updateGroupRo);
        return operate(this::success);
    }

    @PostMapping ("/kickOut")
    @ApiOperation(value = "踢出群聊")
    @LoginRole
    public ResponseEntity<R<Void>> kickOut(KickOutRo kickOutRo){
        groupService.kickOut(getCurrentUserId(), kickOutRo);
        return operate(this::success);
    }
}
